package com.calvin.study.service.sys.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.calvin.study.mapper.xml.SysUserMapper;
import com.calvin.study.model.base.ResponseEntity;
import com.calvin.study.model.sys.Password;
import com.calvin.study.model.sys.SysUser;
import com.calvin.study.service.sys.ISysUserService;
import com.calvin.study.utils.EncryptUtil;

@Service("sysUserService")
public class SysUserService implements ISysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;

	public ResponseEntity doRegister(String username, String password) throws Exception {
		try {
			SysUser sysUser = new SysUser();
			sysUser.setUserName(username);
			Password pwd = EncryptUtil.JointownEncode(password);
			sysUser.setPassword(pwd.getPassword());
			sysUser.setSalt(pwd.getSalt());
			sysUserMapper.insert(sysUser);

			return ResponseEntity.success("注册成功");
		} catch (Exception e) {
			return ResponseEntity.error("注册失败");
		}
	}
	
	public SysUser getByUserName(String username) throws Exception {
		QueryWrapper<SysUser> wrapper = new QueryWrapper<SysUser>();
		wrapper.eq("user_name", username);
		return sysUserMapper.selectOne(wrapper);
	}

}
